package com.service;


import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TimeTask {

    @Autowired
    private RedissonClient redissonClient;

    @Scheduled(cron = "0/10 * * * * ?")

    public void sendMessage() throws InterruptedException {
        //实例化一把锁
        RLock  rLock = redissonClient.getLock("HappyLock");
        //如果3秒内，我没有等待上厕所，我就放弃，f=false,否则f=true
        //我最多占用厕所5秒，如果我5秒没出来，系统自动T我出来
        boolean f = rLock.tryLock(3000,5000, TimeUnit.MILLISECONDS);
        if (f) {
            System.out.println("我获取锁了，准备发送信息");
            System.out.println("Happy birthday");
            Thread.sleep(4000);
        }
        else {
            System.out.println("我没有获取锁,就要退出了");
        }
    }
}
